<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="合同编号" prop="contractNumber">
      <el-input v-model="dataForm.contractNumber" placeholder="合同编号"></el-input>
    </el-form-item>
    <el-form-item label="合同名称" prop="contractName">
      <el-input v-model="dataForm.contractName" placeholder="合同名称"></el-input>
    </el-form-item>
    <el-form-item label="合同开始日期" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="合同开始日期"></el-input>
    </el-form-item>
    <el-form-item label="合同结束日期" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="合同结束日期"></el-input>
    </el-form-item>
    <el-form-item label="合同签订日期" prop="signingDate">
      <el-input v-model="dataForm.signingDate" placeholder="合同签订日期"></el-input>
    </el-form-item>
    <el-form-item label="合同金额" prop="contractPrice">
      <el-input v-model="dataForm.contractPrice" placeholder="合同金额"></el-input>
    </el-form-item>
    <el-form-item label="录入时间" prop="inputTime">
      <el-input v-model="dataForm.inputTime" placeholder="录入时间"></el-input>
    </el-form-item>
    <el-form-item label="录入人（登录的用户）" prop="inputPerson">
      <el-input v-model="dataForm.inputPerson" placeholder="录入人（登录的用户）"></el-input>
    </el-form-item>
    <el-form-item label="合同状态 1：在库 2：出借" prop="contractType">
      <el-input v-model="dataForm.contractType" placeholder="合同状态 1：在库 2：出借"></el-input>
    </el-form-item>
    <el-form-item label="甲方用人单位表的 id" prop="firstPartyId">
      <el-input v-model="dataForm.firstPartyId" placeholder="甲方用人单位表的 id"></el-input>
    </el-form-item>
    <el-form-item label="乙方员工表 id" prop="secondPartyId">
      <el-input v-model="dataForm.secondPartyId" placeholder="乙方员工表 id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          contractNumber: '',
          contractName: '',
          startTime: '',
          endTime: '',
          signingDate: '',
          contractPrice: '',
          inputTime: '',
          inputPerson: '',
          contractType: '',
          firstPartyId: '',
          secondPartyId: ''
        },
        dataRule: {
          contractNumber: [
            { required: true, message: '合同编号不能为空', trigger: 'blur' }
          ],
          contractName: [
            { required: true, message: '合同名称不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '合同开始日期不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '合同结束日期不能为空', trigger: 'blur' }
          ],
          signingDate: [
            { required: true, message: '合同签订日期不能为空', trigger: 'blur' }
          ],
          contractPrice: [
            { required: true, message: '合同金额不能为空', trigger: 'blur' }
          ],
          inputTime: [
            { required: true, message: '录入时间不能为空', trigger: 'blur' }
          ],
          inputPerson: [
            { required: true, message: '录入人（登录的用户）不能为空', trigger: 'blur' }
          ],
          contractType: [
            { required: true, message: '合同状态 1：在库 2：出借不能为空', trigger: 'blur' }
          ],
          firstPartyId: [
            { required: true, message: '甲方用人单位表的 id不能为空', trigger: 'blur' }
          ],
          secondPartyId: [
            { required: true, message: '乙方员工表 id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/contract/schoolcontract/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.contractNumber = data.schoolContract.contractNumber
                this.dataForm.contractName = data.schoolContract.contractName
                this.dataForm.startTime = data.schoolContract.startTime
                this.dataForm.endTime = data.schoolContract.endTime
                this.dataForm.signingDate = data.schoolContract.signingDate
                this.dataForm.contractPrice = data.schoolContract.contractPrice
                this.dataForm.inputTime = data.schoolContract.inputTime
                this.dataForm.inputPerson = data.schoolContract.inputPerson
                this.dataForm.contractType = data.schoolContract.contractType
                this.dataForm.firstPartyId = data.schoolContract.firstPartyId
                this.dataForm.secondPartyId = data.schoolContract.secondPartyId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/contract/schoolcontract/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'contractNumber': this.dataForm.contractNumber,
                'contractName': this.dataForm.contractName,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'signingDate': this.dataForm.signingDate,
                'contractPrice': this.dataForm.contractPrice,
                'inputTime': this.dataForm.inputTime,
                'inputPerson': this.dataForm.inputPerson,
                'contractType': this.dataForm.contractType,
                'firstPartyId': this.dataForm.firstPartyId,
                'secondPartyId': this.dataForm.secondPartyId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
